<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="姓名" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="性别" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
              <j-dict-select-tag v-model="model.gender" placeholder="请选择性别" dictCode="sex" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birth">
              <j-date placeholder="请选择出生日期" v-model="model.birth" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系电话" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mail">
              <a-input v-model="model.mail" placeholder="请输入邮箱"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="image">
              <j-image-upload class='avatar-uploader' text='上传头像' v-model='model.image'></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="个人简介" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intro">
              <a-textarea v-model="model.intro" rows="4" placeholder="请输入个人简介" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属医院" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgId">
              <a-select v-model="model.orgId" placeholder="请选择所属医院" @change="changeOrg">
                <a-select-option :value="i.id" v-for="(i, o) in hospList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属科室" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dept">
              <a-select v-model="model.dept" placeholder="请选择所属科室" :disabled="!model.orgId">
                <a-select-option :value="i.id" v-for="(i, o) in deptList" :key="o">{{ i.deptName }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="资质" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cert">
              <a-input v-model="model.cert" placeholder="请输入资质"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="专长" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="speciality">
              <a-input v-model="model.speciality" placeholder="请输入专长"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="从业时间" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workingTime">
              <a-input v-model="model.workingTime" placeholder="请输入从业时间"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <a-input v-model="model.userId" placeholder="请输入用户" :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="账号"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="username">
              <a-input v-model="model.username" placeholder="请输入账号"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WzDoctorInfoForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      deptList: [],
      hospList: [],
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: '请输入医生名称!' }],
        gender: [{ required: true, message: '请选择医生性别!' }],
        phone: [{ required: true, message: '请输入联系方式!' }],
        intro: [{ required: true, message: '请输入个人简介!' }],
        orgId: [{ required: true, message: '请选择所属医院!' }],
        dept: [{ required: true, message: '请选择所属科室!' }],
        cert: [{ required: true, message: '请输入资质!' }],
        speciality: [{ required: true, message: '请输入专长!' }],
        workingTime: [{ required: true, message: '请输入从业时间!' }],
      },
      url: {
        add: "/wz/doctor/wzDoctorInfo/add",
        edit: "/wz/doctor/wzDoctorInfo/edit",
        queryById: "/wz/doctor/wzDoctorInfo/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    getAction('/wz/hosp/wzHospital/list', { pageNo: 1, pageSize: 999 }).then(res => {
      this.hospList = res.result.records;
    })

  },
  methods: {
    changeOrg(val) {
      getAction('/wz/hosp/wzHospDept/list', { pageNo: 1, pageSize: 999, orgId: val }).then(res => {
        this.deptList = res.result.records;
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>
<style lang="less" scoped></style>